package com.example.contextcube.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 字母索引视图
 * 用于在列表旁边显示字母索引，点击可快速跳转到对应字母开头的项
 */
public class AlphabetIndexView extends View {
    private static final String TAG = "AlphabetIndexView";
    
    // 默认字母表（26个英文字母）
    private static final String[] DEFAULT_INDEX_ITEMS = {
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"
    };
    
    // 绘制相关参数
    private Paint paint;
    private int textColor = Color.parseColor("#3B82F6");  // 默认文字颜色（浅蓝色）
    private int highlightTextColor = Color.parseColor("#1E3A8A");  // 高亮文字颜色（深蓝色）
    private float textSize = 14.0f;  // 默认文字大小
    
    // 字母索引数据
    private String[] indexItems = DEFAULT_INDEX_ITEMS;
    
    // 当前选中的索引
    private int currentIndex = -1;
    
    // 监听器
    private OnIndexTouchListener listener;
    
    public AlphabetIndexView(Context context) {
        super(context);
        init();
    }
    
    public AlphabetIndexView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public AlphabetIndexView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(textSize * getResources().getDisplayMetrics().density);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        // 计算每个字母的高度
        int height = getHeight();
        int width = getWidth();
        int singleHeight = height / indexItems.length;
        
        // 绘制每个字母
        for (int i = 0; i < indexItems.length; i++) {
            // 根据是否选中设置颜色
            paint.setColor(currentIndex == i ? highlightTextColor : textColor);
            // 选中项加粗
            paint.setTypeface(currentIndex == i ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
            
            // 计算文字宽高
            Rect rect = new Rect();
            paint.getTextBounds(indexItems[i], 0, indexItems[i].length(), rect);
            float xPos = (width - rect.width()) / 2;
            float yPos = singleHeight * i + (singleHeight + rect.height()) / 2;
            
            // 绘制文字
            canvas.drawText(indexItems[i], xPos, yPos, paint);
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                // 计算触摸的是哪个字母
                float y = event.getY();
                int index = (int) (y / getHeight() * indexItems.length);
                
                // 确保索引在有效范围内
                if (index < 0) {
                    index = 0;
                } else if (index >= indexItems.length) {
                    index = indexItems.length - 1;
                }
                
                // 触摸位置改变且监听器不为空时回调
                if (currentIndex != index && listener != null) {
                    listener.onIndexTouched(indexItems[index], index);
                }
                
                currentIndex = index;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                currentIndex = -1;
                invalidate();
                break;
        }
        return true;
    }
    
    /**
     * 设置字母索引项
     */
    public void setIndexItems(String[] indexItems) {
        this.indexItems = indexItems;
        invalidate();
    }
    
    /**
     * 设置文字颜色
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
        invalidate();
    }
    
    /**
     * 设置高亮文字颜色
     */
    public void setHighlightTextColor(int highlightTextColor) {
        this.highlightTextColor = highlightTextColor;
        invalidate();
    }
    
    /**
     * 设置文字大小（dp）
     */
    public void setTextSize(float textSize) {
        this.textSize = textSize;
        paint.setTextSize(textSize * getResources().getDisplayMetrics().density);
        invalidate();
    }
    
    /**
     * 设置索引触摸监听器
     */
    public void setOnIndexTouchListener(OnIndexTouchListener listener) {
        this.listener = listener;
    }
    
    /**
     * 索引触摸监听器接口
     */
    public interface OnIndexTouchListener {
        /**
         * 当索引被触摸时回调
         * 
         * @param index 触摸的字母
         * @param position 触摸的位置
         */
        void onIndexTouched(String index, int position);
    }
} 